package com.shanzhi.dify.service.impl;

import com.shanzhi.connection.common.ConnectionUtils;
import com.shanzhi.connection.common.HttpRequestType;
import com.shanzhi.dify.common.DifyUrlEnums;
import com.shanzhi.dify.entity.ChunkRequestParam;
import com.shanzhi.dify.entity.ChunkUpdateParam;
import com.shanzhi.dify.service.DifyBaseService;
import com.shanzhi.dify.service.DifyChunkService;
import kong.unirest.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文档片段相关接口
 */
@Service
@Slf4j
public class DifyChunkServiceImpl implements DifyChunkService {

    @Autowired
    private DifyBaseService difyBaseService;


    @Override
    public String getDocumentChunks(ChunkRequestParam chunkRequestParam) {
        Map<String, Object> params = new HashMap<>();
        params.put("page", chunkRequestParam.getPage());
        params.put("limit", chunkRequestParam.getLimit());
        // 如果参数不为空，那么注入入参
        if (StringUtils.hasText(chunkRequestParam.getKeyword())) {
            params.put("keyword", chunkRequestParam.getKeyword());
        }
        return difyBaseService.sendDifyRequest(null, null, DifyUrlEnums.LIST_CHUNKS_URL, HttpRequestType.GET, params, chunkRequestParam.getDatasetId(), chunkRequestParam.getDocumentId());
    }


    @Override
    public String deleteDocumentChunks(String datasetId, String documentId, String chunkId) {
        String url = difyBaseService.buildUrl(DifyUrlEnums.DELETE_CHUNK_URL, datasetId, documentId, chunkId);
        String apiKey = difyBaseService.resolveApiKey(null);
        return ConnectionUtils
                .sendInternal(null, url, headers -> headers.put("Authorization", "Bearer " + apiKey), HttpRequestType.DELETE, null, null);
    }

    @Override
    public void editDocumentChunks(String datasetId, String documentId, String segmentId, ChunkUpdateParam chunkUpdateParam) {
        String url = difyBaseService.buildUrl(DifyUrlEnums.DELETE_CHUNK_URL, datasetId, documentId, segmentId);
        String apiKey = difyBaseService.resolveApiKey(null);

        Map<String, Object> requestBody = new HashMap();
        requestBody.put("segment", chunkUpdateParam);

        ConnectionUtils
                .sendInternal(JSONObject.valueToString(requestBody), url, headers -> headers.put("Authorization", "Bearer " + apiKey), HttpRequestType.POST, null, null);
    }


    @Override
    public String addDocumentChunks(String datasetId, String documentId, List<ChunkUpdateParam> chunkUpdateParams) {
        String url = difyBaseService.buildUrl(DifyUrlEnums.LIST_CHUNKS_URL, datasetId, documentId);
        String apiKey = difyBaseService.resolveApiKey(null);

        // 构造正确的请求体结构
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("segments", chunkUpdateParams);

        return ConnectionUtils
                .sendInternal(com.alibaba.fastjson.JSONObject.toJSONString(requestBody), url, headers -> headers.put("Authorization", "Bearer " + apiKey), HttpRequestType.POST, null, null);
    }
}
